const express = require('express');
const loginMiddleware = require("../middleware/LoginMiddleware");

const GroupsPeopleDao = require('../database/GroupsPeopleDao')

const router = express.Router();

router.post("/:type", loginMiddleware, async (req, res) => {
    let type = req.params.type;
    let group_people = req.body;
    try {
        let result = await GroupsPeopleDao.addGroupsPeople(group_people.group_id, type, group_people.user_id);
        console.log("GroupsPeople添加的结果/:type是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "添加成功", data: null });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "添加失败", data: null });
    }
})

router.delete("/groupId/:GroupId",loginMiddleware,async (req, res) => {
    let GroupId = req.params.GroupId;
    try {
        let result = await GroupsPeopleDao.deleteGroupsPeopleByGroupID(GroupId);
        console.log("GroupsPeople删除的结果/groupId/:GroupId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "删除成功", data: null });
    }
    catch (err) {
        console.log(err);
        res.send({code: 5000, msg: "删除失败", data: null });
    }
})

router.delete("/:GroupsPeopleId", loginMiddleware, async (req, res) => {
    let group_people_id = req.params.GroupsPeopleId;
    try {
        let result = await GroupsPeopleDao.deleteGroupsPeopleById(group_people_id);
        console.log("GroupsPeople删除的结果/:GroupsPeopleId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "删除成功", data: null });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "删除失败", data: null });
    }
})

router.get("/type/:GroupID", loginMiddleware, async (req, res) => {
    let groupID = req.params.GroupID;
    try {
        let result = await GroupsPeopleDao.selectGroupsPeopleByTypeAndGroupID(groupID);
        console.log("GroupsPeople查询的结果/type/:GroupID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
   }
   catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
   }
})

router.get("/userId/:UserID", loginMiddleware, async (req, res) => {
    let userId = req.params.UserID;
    try {
        let result = await GroupsPeopleDao.selectGroupsPeopleByUserID(userId);
        console.log("GroupsPeople查询的结果/userId/:UserID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
   }
   catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败11", data: null });
   }
})

router.get("/groupPeopleId/:GroupPeopleId", loginMiddleware, async (req, res) => {
    let groupPeopleId = req.params.GroupPeopleId;
    try {
        let result = await GroupsPeopleDao.selectGroupsPeopleByGroupPeopleId(groupPeopleId);
        console.log("GroupsPeople查询的结果/groupPeopleId/:GroupPeopleId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "服务器错误", data: err });
    }
})

router.get("/:GroupID", loginMiddleware, async (req, res) => {
    let GroupID = req.params.GroupID;
    try {
        let result = await GroupsPeopleDao.selectGroupsPeopleByGroupID(GroupID);
        console.log("GroupsPeople查询的结果/:GroupID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

router.get("/:GroupID/:UserID", loginMiddleware, async (req, res) => {
    let GroupID = req.params.GroupID;
    let UserID = req.params.UserID;
    try {
        let result = await GroupsPeopleDao.selectGroupsPeopleByGroupIDAndUserID(GroupID, UserID);
        console.log(`GroupsPeople查询的结果/:GroupID=${GroupID}/:UserID=${UserID}是:\n` +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch (err) {
        console.log(err);
       res.send({ code: 5000, msg: "查询失败",data: null });
    }
})

router.put("/", loginMiddleware, async (req, res) => {
    let groupPeople = req.body;
    try {
        let result = await GroupsPeopleDao.updataType(groupPeople.group_people_id, groupPeople.type);
        console.log("修改的结果是:\n" + result + "\n————\n");
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "修改失败", data: null });
    }
})

module.exports = router;